package com.atguigu.minio;

import com.atguigu.minio.config.MinioProperties;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.SetBucketPolicyArgs;
import io.minio.errors.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


// Generated by https://start.springboot.io
// 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn
@SpringBootApplication
public class Demo01MinioApplication {

    public static void main(String[] args) {
        SpringApplication.run(Demo01MinioApplication.class, args);
    }
    //项目启动时初始化minio客户端对象注入到容器中

//    @Bean
//    public MinioClient minioClient() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
//        String bucket = MinioProperties.BUCKET;
//        //Builder构建者设计模式
//        MinioClient client = MinioClient.builder()
//                //账号密码
//                .credentials(MinioProperties.ACCESS_KEY, MinioProperties.SECRET_KEY)
//                //minio服务端 接收java客户端请求的地址： 9000 api接口地址
//                .endpoint(MinioProperties.ENDPOINT)
//                //构建
//                .build();
//
//        boolean exists = client.bucketExists(BucketExistsArgs.builder()
//                .bucket(bucket)
//                .build());
//        if(!exists){
//            //桶不存在，创建桶
//            client.makeBucket(MakeBucketArgs.builder()
//                    .bucket(bucket)
//                    .build());
//            String policyJson = "{\n" +
//                    "    \"Version\": \"2012-10-17\",\n" +
//                    "    \"Statement\": [\n" +
//                    "        {\n" +
//                    "            \"Effect\": \"Allow\",\n" +
//                    "            \"Principal\": {\n" +
//                    "                \"AWS\": [\n" +
//                    "                    \"*\"\n" +
//                    "                ]\n" +
//                    "            },\n" +
//                    "            \"Action\": [\n" +
//                    "                \"s3:GetBucketLocation\",\n" +
//                    "                \"s3:ListBucket\",\n" +
//                    "                \"s3:ListBucketMultipartUploads\"\n" +
//                    "            ],\n" +
//                    "            \"Resource\": [\n" +
//                    "                \"arn:aws:s3:::"+bucket+"\"\n" +
//                    "            ]\n" +
//                    "        },\n" +
//                    "        {\n" +
//                    "            \"Effect\": \"Allow\",\n" +
//                    "            \"Principal\": {\n" +
//                    "                \"AWS\": [\n" +
//                    "                    \"*\"\n" +
//                    "                ]\n" +
//                    "            },\n" +
//                    "            \"Action\": [\n" +
//                    "                \"s3:AbortMultipartUpload\",\n" +
//                    "                \"s3:DeleteObject\",\n" +
//                    "                \"s3:GetObject\",\n" +
//                    "                \"s3:ListMultipartUploadParts\",\n" +
//                    "                \"s3:PutObject\"\n" +
//                    "            ],\n" +
//                    "            \"Resource\": [\n" +
//                    "                \"arn:aws:s3:::"+bucket+"/*\"\n" +
//                    "            ]\n" +
//                    "        }\n" +
//                    "    ]\n" +
//                    "}";
//            //给桶设置权限
//
//            client.setBucketPolicy(SetBucketPolicyArgs.builder()
//                    //桶名称
//                    .bucket(bucket)
//                    //权限字符串
//                    .config(policyJson)
//                    .build());
//        }
//
//        return client;
//    }
}
